package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.activity.ChatActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/21.
 */
public class DetailBmrGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<OrderInfo> mList = new ArrayList<OrderInfo>();

    public DetailBmrGridAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<OrderInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.detail_fans_item,null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_icon);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

       final  OrderInfo  fansInfo = mList.get(position);
        if(fansInfo != null){
            String headImgUrl = fansInfo.getUserLogo();

            if(headImgUrl != null && headImgUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg,headImgUrl,new ViewUtil.Size(ViewUtil.dip2px(mContext,40),ViewUtil.dip2px(mContext,40)));
            }
            if(fansInfo.getNickName()!=null){
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(fansInfo.getNickName(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }


//            childHolder.headImg.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    //TODO 发消息界面
//                    String Uid = fansInfo.getUserId().toLowerCase();
//                    String opid = fansInfo.getOpenId();
//                    if(opid!=null){
//                        Uid = opid.toLowerCase();
//                    }
//                    EatShowUserInfo myInfo = EatShowApp.getInstance().getmUserInfo();
//                    String myId = TextUtils.isEmpty(myInfo.getOpenId()) ? myInfo.getUserId() : myInfo.getOpenId();
//                    if (myId.equalsIgnoreCase(Uid)){
//                        ToastUtil.showMessage(mContext,R.string.Cant_chat_with_yourself);
//                        return;
//                    }
//                    Intent intent = new Intent(mContext,ChatActivity.class);
//                    intent.putExtra("userId",Uid);
//                    intent.putExtra("userNick",fansInfo.getNickName());
//                    intent.putExtra("fromLogo", fansInfo.getUserLogo());
//                    mContext.startActivity(intent);
//                }
//            });
        }

        return convertView;
    }

    class ChildHolder{
        ImageView headImg;
        TextView nickNameText;
    }
}
